const request = require('request-promise');
const miniPubAcc = require('./config.js')

async function getTokenApi() {
  console.log("getTokenApi" + JSON.stringify(miniPubAcc))
  let token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential" +
      `&appid=${miniPubAcc.appid}&secret=${miniPubAcc.secret}`
  const options = {method: 'GET', uri: token_url,};
  let result = await request(options)
  return result
}

async function genWxShortLink() {
  let access_token = await getTokenApi()
  let page_url = ''
  let is_permanent = true
  let gen_shortlink_url = `https://api.weixin.qq.com/wxa/genwxashortlink?access_token=${access_token}&page_url=${page_url}&is_permanent=${is_permanent}`
  const options = {method: 'POST', uri: gen_shortlink_url};
  let result = await request(options)
  return result
}

module.exports = {
  genWxShortLink
};
